package com.ken.callblock2.activity;

import com.ken.callblock2.R;
import com.ken.callblock2.view.BlockListFragment;
import com.ken.callblock2.view.MainViewFragment;
import com.ken.callblock2.view.MenuFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;

public class MainActivity extends BaseActivity {
	
	protected 	ListFragment mFrag;
	private 	Fragment mContent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent it = new Intent();
		it.setAction("com.ken.callblock2.receiver.BootBroadcastReceiver");
		sendBroadcast(it);
		
		setSlidingActionBarEnabled(true);
		setContentView(R.layout.frame_main_layout);
		// 設定首頁VIEW
		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
		if (mContent == null)
			mContent = new MainViewFragment();
		//
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.main_content, mContent)
		.commit();
		// 設定左邊MENU VIEW
		setBehindContentView(R.layout.frame_menu_left);
		if (savedInstanceState == null) {
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			mFrag = new MenuFragment();
			t.replace(R.id.frame_left, mFrag);
			t.commit();
		} else {
			mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.main_content);
		}
		// 設定封鎖名單VIEW
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.frame_right, new BlockListFragment())
		.commit();		
		
	}
	
	public void switchContent(Fragment fragment) {
		if (fragment.getClass().getSimpleName().equals(mContent.getClass().getSimpleName())) {
			toggle();
			return;
		}
		mContent = fragment;
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.main_content, fragment)
		.commit();
		getSlidingMenu().showContent();
	}
}
